package com.yu.crm.utils;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.net.ftp.FTPClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;


/**
 * 调用API接口判断日期是否是工作日 周末还是节假日
 * 工作日对应结果为 0, 休息日对应结果为 1, 节假日对应的结果为 2
 */
public class HolidayUtil {
    /**
     * 传入yyyyMMdd判断今天是否是0 上班 1周末 2节假日
     *
     * @param httpArg
     * @return
     */
    public static Integer request(String httpArg) {
        httpArg = httpArg.replace("-", "");

        String httpUrl = "http://www.easybots.cn/api/holiday.php";
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        httpUrl = httpUrl + "?d=" + httpArg;

        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
            ObjectMapper mapper = new ObjectMapper();
            HashMap<String, Object> map = mapper.readValue(result, new TypeReference<HashMap<String, Object>>() {
            });
            return Integer.parseInt(map.get(httpArg).toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        //判断今天是否是工作日 周末 还是节假日
        System.out.println(request("2020-02-01"));
    }


}
